<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Management extends MX_Controller {

	public function __construct() {
		parent::__construct();
		$config = array('table'=>'category_tree');
		$this->load->library('mahana_hierarchy');
		$this->mahana_hierarchy->initialize($config);

		$this->load->model('admin_category_tree_model');
		$this->load->model('admin_counter_model');
		$this->load->helper('url');
		//$this->load->library('pagination');
		$this->load->library('pagination');
		$this->template->set_theme('admin');
		$this->template->set_layout('default');
		$this->template->set_partial('side_menu','layouts/side_menu');
		$this->template->set_partial('nav_bar','layouts/nav_bar');
		$this->template->set_partial('footer','footer/footer');

	}


	public function show(){

		$data['category']['all'] = $this->admin_category_tree_model->get_cetegory();

		$this->template->build('category/category_tree',$data);

	}

	function add()
	{
		$data['mode'] = "ADD";
		$this->template->build('category/add_keyword',$data);

	}

function insert()
	{
		$this->form_validation->set_rules('title','title','required|xss_clean|is_unique[category_tree.name]');
		$this->form_validation->set_rules('url','Url','xss_clean|required|is_unique[category_tree.url]');
		$this->form_validation->set_rules('description','Description','xss_clean|is_unique[category_tree.description]');
		$this->form_validation->set_rules('meta_title','Meta Title','xss_clean|is_unique[category_tree.meta_title]');
		$this->form_validation->set_rules('meta_keyword','Meta Keyword','xss_clean|is_unique[category_tree.meta_keyword]');
		$this->form_validation->set_rules('meta_description','Meta Description','xss_clean|is_unique[category_tree.meta_description]');
		
		
		
		if($this->form_validation->run()){

			
			// add a child
			$data['name'] = $this->input->post('title');
			$data['url'] = $this->input->post('url');
			$data['description'] = $this->input->post('description');
				
			$data['meta_title'] = $this->input->post('meta_title');
			$data['meta_keyword'] = $this->input->post('meta_keyword');
			$data['meta_description'] = $this->input->post('meta_description');
				
			//$data['parent_id'] = $this->input->post('parent_id');
			
			
			$this->mahana_hierarchy->insert($data);
			
			
			//$this->mahana_hierarchy->resync();
			
			$this->session->set_flashdata('message', 'Keyword Added Sucessfully!... ');
			redirect('secure/category_tree/management/add/');
				
			
		}
		else{
			$this->session->set_flashdata('message', 'Fields Can not be null!... ');
			$this->add();	
			//redirect('secure/category_tree/management/add/');
			
				
		}
	}
	
	function edit($id){
		
		$data['mode'] = "EDIT";
		$data['category']['info'] = $this->admin_category_tree_model->get_cetegory_data($id);
		$data['id'] = $id;
		//die();
		
		$this->template->build('category/add_keyword',$data);
	}
	
	function update(){
		
		$this->form_validation->set_rules('title','title','required|xss_clean');
		
		if($this->form_validation->run()){
			$post_data =array(
					'name' =>  $this->input->post('title'),
					'url' =>  $this->input->post('url'),
					'description' => $this->input->post('description'),
					'meta_title' =>  $this->input->post('meta_title'),
					'meta_keyword' =>  $this->input->post('meta_keyword'),
					'meta_description' =>  $this->input->post('meta_description'),
					
					);
			
			
			$this->db->where('id',$this->input->post('id'));
			$this->db->update('category_tree',$post_data);
			$this->session->set_flashdata('message', 'Keyword updated Sucessfully!... ');
			redirect('secure/category_tree/management/edit/'.$this->input->post('id'));
				
				
		}
		
	}
	
	function delete($category_id) {
		$this->admin_category_tree_model->category_delete($category_id);
		redirect('secure/category_tree/management/show');
	}

}